<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*
 * @author: Manh Nguyen
 * @email: manhnv@binhhoang.com
 * @date: 17.04.2012
 */

class Location extends CI_Controller {
	
	function __construct()
	{
		parent::__construct();
		$this->load->model(array('result_model','location_model'));
		$this->load->helper('result');
	}
	
	public function index()
	{
		$data	=	array();
		$data['tmpl']	=	'client/location/index';
		$this->load->view('client/layout/content', $data);
	}
	
	public function detail($alias = null, $date = NULL)
	{
		$data	=	array();
		$alias	=	strtolower($alias);
		if($alias == 'xo-so-mien-nam' || $alias == 'xo-so-mien-trung')
		{
			$area = '';
			switch($alias)
			{
				case 'xo-so-mien-trung':
					$area = 'MT';
					break;
				case 'xo-so-mien-nam':
					$area = 'MN';
					break;
				default:
					$area = 'MB';
			}
			$locations	=	$this->location_model->get_locations_by_area($area);
			$data['locations']	=	$locations;
			$data['area']	=	$area;
			$data['tmpl']	=	'client/location/index';
		}
		else
		{
			$result = array();
			$location	=	$this->location_model->get_by(array('alias' => $alias));
			if($location)
			{
				$max_date	=	$this->result_model->order_by('date','DESC')->limit(1,0)->get_by(array('lid' => $location->id))->date;
				if(!$date)
				{
					$result = $this->result_model->order_by('date','DESC')->limit(1,0)->get_by(array('lid' => $location->id));
				}
				else
				{
					$date	=	date('Y-m-d',strtotime($date));
					$result = $this->result_model->order_by('date','DESC')->limit(1,0)->get_by(array('lid' => $location->id,'date' => $date));
				}
				$d = $this->getDateOfWeekKT($result->date);
				$cd = $location->code;
				$numday = "";
				if($cd == "HCM" && $d=='T2')
				{
					$numday = 5;
					$numday2 = 2;
				}
				elseif($cd == "HCM" && $d!='T2')
				{
					$numday = 2;
					$numday2 = 5;
				}
				elseif($cd == "DNG" && $d=='T4')
				{
					$numday = 3;
					$numday2 = 4;
				}
				elseif($cd == "DNG" && $d=='T7')
				{
					$numday = 4;
					$numday2 = 3;
				}
				elseif($cd == "MB")
				{
					$numday = 1;
					$numday2 = 1;
				}
				else {
					$numday = $numday2 =7;
				}
				
				$dt = date('Y-m-d',strtotime("{$result->date} +$numday days"));
				if(strtotime($dt) > strtotime(date('Y-m-d')))
				{
					$result->fl = "1";
				}
				
				$result->link1 .= $location->alias;
				$result->linkday1 = date('d-m-Y',strtotime("{$result->date} -$numday2 day"));
				if(date('Y-m-d',strtotime("{$result->date} +$numday day")) <= $max_date){
					$result->link2 .= $location->alias;
					$result->linkday2 = date('d-m-Y',strtotime("{$result->date} +$numday day"));
				}else{
					$result->link2 .= "#";
					$result->linkday2 = date('d-m-Y',strtotime("{$result->date} +$numday day"));
				}
				//print_r($result);
			}
			$data['result']	=	$result;
			$data['tmpl']	=	'client/location/view';
			$data['location']	=	$location;
		}
		
		$this->load->view('client/layout/content', $data);
	}
	
	//Thong ke theo so lan quay
	function statistics($alias = '', $days = 10)
	{
		$this->load->model('mobile_statistics_model');
		$location	=	$this->location_model->get_by(array('alias' => $alias));
		if($location)
		{
			$_data	=	$this->mobile_statistics_model->order_by('update_date','DESC')->get_by(array('lid' => $location->id, 'num_statistics' => $days));
			$result =	json_decode($_data->result);
			$data['result']	=	$result;
		}
		
		$data['days']	=	$days;
		$data['tmpl']	=	'client/location/statistics';
		$this->load->view('client/layout/content', $data);
	}
	//Thong ke theo so lan quay
	
	/**
	 * Tra cuu ket qua
	 */
	function find($alias = '')
	{
		$input_form	=	array();
		$result		=	array();
		
		$location	=	$this->location_model->get_by(array('alias' => $alias));
		if($location)
		{
			$input_form['lid']	=	$location->id;
		}
		if($_SERVER['REQUEST_METHOD'] == "POST")
		{
			$number	=	$this->input->post('number');
			$lid	=	(int)$this->input->post('lid');
			$day	=	(int)$this->input->post('day');
			$month	=	(int)$this->input->post('month');
			$year	=	(int)$this->input->post('year');
			
			$input_form['number']	=	$number;
			$input_form['lid']		=	$lid;
			$input_form['day']		=	$day;
			$input_form['month']	=	$month;
			$input_form['year']		=	$year;
			
			if($number >=0 && $lid !=0 && $day > 0 && $month > 0 && $year > 0)
			{
				$result	=	$this->result_model->get_by(array('lid' => $lid, 'date' => $year.'-'.$month.'-'.$day));
				if($result)
				{
					$result->lo[] = $this->dem($result->b0,$number);
					$result->lo[] = $this->dem($result->b1,$number);
					$result->lo[] = $this->dem($result->b2,$number);
					$result->lo[] = $this->dem($result->b3,$number);
					$result->lo[] = $this->dem($result->b4,$number);
					$result->lo[] = $this->dem($result->b5,$number);
					$result->lo[] = $this->dem($result->b6,$number);
					$result->lo[] = $this->dem($result->b7,$number);
					$result->lo[] = $this->dem($result->b8,$number);
					//$result->lo[] = $this->dem($b0);
				}
			}
			
		}
		
		$data['result']		=	$result;
		$data['input_form']	=	$input_form;
		$data['locations']	=	$this->location_model->get_many_by(array('status' => 1));
		$location	=	$this->location_model->get_by(array('alias' => $alias));
		$data['tmpl']	=	'client/location/find';
		$this->load->view('client/layout/content', $data);
	}
	//Tra cuu ket qua
	
	function dem($str = '',$num = null)
	{
		$arr	=	explode(',',$str);
		$count = 0;
		foreach($arr as $k => $v)
		{
			if($v == $num)
			{
				$count++;
			}
		}
		
		return $count;
	}
	
	function getDateOfWeekKT($date = null) 
	{
		$date = date('D',strtotime("$date "));
		$list = array(
			'Mon' => 'T2',
			'Tue' => 'T3',
			'Wed' => 'T4',
			'Thu' => 'T5',
			'Fri' => 'T6',
			'Sat' => 'T7',
			'Sun' => 'CN',
		);
		return $list[$date];
	}
}

/* End of file home.php */
/* Location: ./application/controllers/home.php */